package com.liu.QQ.client.service;

import com.liu.QQ.Message;
import com.liu.QQ.MessageType;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Date;

/**
 *提供消息相关的服务方法
 * @author liuchengjie
 */
public class MessageClientService {

    /**
     * 给某个用户发送消息
     * @param senderId 发送方
     * @param getterId 接收方
     * @param content 发送内容
     */
    public void sendMessageToOne(String senderId,String getterId,String content) {
        //构建message对象
        Message message = new Message();
        message.setSender(senderId);
        message.setGetter(getterId);
        message.setContent(content);
        message.setSendTime(new Date().toString());
        message.setMesType(MessageType.MESSAGE_COMM_MES);
        //发送给服务端
        try {
            ObjectOutputStream outputStream = new ObjectOutputStream(ManageClientConnectThread
                    .getConnectThread(senderId).getSocket().getOutputStream());
            outputStream.writeObject(message);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 群发消息
     * @param sender
     * @param content
     */
    public void distributionMessage(String sender,String content) {
        Message message = new Message();
        message.setSender(sender);
        message.setContent(content);
        message.setMesType(MessageType.MESSAGE_DISTRIBUTION);
        //发送给服务端
        try {
            ObjectOutputStream outputStream = new ObjectOutputStream(ManageClientConnectThread
                    .getConnectThread(sender).getSocket().getOutputStream());
            outputStream.writeObject(message);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
